package cn.forward.config;


import cn.dev33.satoken.secure.BCrypt;
import cn.forward.constants.Constants;
import cn.forward.pojo.domain.Login;
import cn.forward.service.ILoginService;
import java.util.Date;

import cn.forward.utils.NameUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;


@Component
public class AdminUerName implements ApplicationListener<ContextClosedEvent> {

    @Autowired
    ILoginService loginService;


    @Override
    public void onApplicationEvent(ContextClosedEvent event) {


        Login adminLogin = loginService.getOne(new LambdaQueryWrapper<Login>().eq(Login::getUsername, "admin")
                .eq(Login::getType, 0));
        if (ObjectUtil.isNull(adminLogin)){
            Login login = new Login();
            login.setUsername("admin");

            String pw_hash = BCrypt.hashpw("123456", BCrypt.gensalt());
            login.setPassword(pw_hash);
            login.setType(0);
            login.setEnabled(true);
            login.setAvatar("");
            login.setAdmin(true);
            login.setNickName(NameUtil.getName());
            login.setName(NameUtil.getName());
            login.setPhone("14525418");
            login.setCreateTime(new Date());
            login.setDeleted(false);
            login.setVersion(0);

            loginService.save(login);
        }






    }
}
